package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.entities.GeneralLedger;

public class GeneralLedgerDAOImpls extends DAO<GeneralLedger> implements GeneralLedgerDAO {
	
	public static final String field_glkey = "GLKey";
	public static final String field_slip_id = "Slip_ID	";
	public static final String field_slipdate = "SlipDate";
	public static final String field_systemDate = "systemDate";
	public static final String field_coakey = "COAKey";
	public static final String field_currencykey = "CurrencyKey";
	public static final String field_currencyratekey = "CurrencyRateKey";
	public static final String field_departmentkey = "DepartmentKey";
	public static final String field_exchangerate = "ExchangeRate";
	public static final String field_sourceamount = "SourceAmount";
	public static final String field_homeamount = "HomeAmount";
	public static final String field_remark = "Remark";
	public static final String field_status = "Status";
	public static final String field_srno = "SrNo";
	public static final String field_jokey = "JobKey";
	public static final String field_cashbookkey = "CashBookKey";
	public static final String field_office_id = "Office_Id";
	
	public GeneralLedgerDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "GL";
	}

	@Override
	public void create(GeneralLedger entity) throws SQLException {
		query = insertParam(table_name, new String[] { field_glkey, field_slip_id, field_slipdate, field_systemDate,
				field_coakey, field_currencykey, field_currencyratekey, field_departmentkey, field_exchangerate, 
				field_sourceamount, field_homeamount, field_remark, field_status, field_srno, field_jokey, field_cashbookkey, field_office_id});
		
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(GeneralLedger entity) throws SQLException {
		query = updateParam(table_name, new String[] { field_glkey, field_slip_id, field_slipdate, field_systemDate,
				field_coakey, field_currencykey, field_currencyratekey, field_departmentkey, field_exchangerate, 
				field_sourceamount, field_homeamount, field_remark, field_status, field_srno, field_jokey, field_cashbookkey, field_office_id}) + " AND " + field_glkey + " = " + entity.getGlKey();
		
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}
	
	protected void updateRecord(GeneralLedger entity) throws SQLException {
		SimpleDateFormat l_format = new SimpleDateFormat("yyyyMMdd");
		
		int i = 1;
		pstmt.setLong(i++, entity.getGlKey());
		pstmt.setString(i++, entity.getSlip_ID());
		pstmt.setString(i++, l_format.format(entity.getSlipDate()));
		pstmt.setString(i++, l_format.format(entity.getSystemDate()));
		pstmt.setLong(i++, entity.getCoaKey());
		pstmt.setLong(i++, entity.getCurrencyKey());
		pstmt.setLong(i++, entity.getCurrencyRateKey());
		pstmt.setLong(i++, entity.getDepartmentKey());
		pstmt.setDouble(i++, entity.getExchangeRate());
		pstmt.setDouble(i++, entity.getSourceAmount());
		pstmt.setDouble(i++, entity.getHomeAmount());
		pstmt.setString(i++, entity.getRemark());
		pstmt.setString(i++, entity.getStatus());
		pstmt.setInt(i++, entity.getSrNo());
		pstmt.setLong(i++, entity.getJobKey());
		pstmt.setLong(i++, entity.getCashbookKey());
		pstmt.setLong(i++, entity.getOffice_ID());
	}

	@Override
	public void delete(String slipNo) throws SQLException {
		query = getDeleteQuery() + " WHERE " + field_slip_id + " = '" + slipNo + "'";
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	protected GeneralLedger readRecord(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
